package model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Bushi
 * 
 */
public class PresentImpl implements Present {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7686430035794448633L;
	int number;
	List<String> names;
	Category category;

	/**
	 * Konstruktor fuer ein neues Geschenk mit Namen und einer Nummer
	 * 
	 * @param number
	 *            die eindeutige Nummer
	 * @param names
	 *            die Namen der Personen, denen dieses Geschenk gefallen koennte
	 */
	public PresentImpl(int number, String[] names, Category category) {
		this.names = new ArrayList<String>(Arrays.asList(names));
		this.number = number;
		this.category = category;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see model.Present#getNumber()
	 */
	@Override
	public int getNumber() {
		return number;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see model.Present#getNames()
	 */
	@Override
	public String[] getNames() {
		return names.toArray(new String[0]);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see model.Present#getCategory()
	 */
	@Override
	public Category getCategory() {
		return category;
	}

	// {(x, y) such that x.compareTo(y) <= 0}.
	@Override
	public int compareTo(Present p) {
		if (this.equals(p)) {
			return 0;
		} else if (getNumber() < p.getNumber())
			return -1;
		else {
			return 1;
		}
	}

	@Override
	public boolean isForParticipant(String name) {
		return names.contains(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((category == null) ? 0 : category.hashCode());
		result = prime * result + ((names == null) ? 0 : names.hashCode());
		result = prime * result + number;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof PresentImpl)) {
			return false;
		}
		PresentImpl other = (PresentImpl) obj;
		if (category != other.category) {
			return false;
		}
		if (names == null) {
			if (other.names != null) {
				return false;
			}
		} else if (!names.equals(other.names)) {
			return false;
		}
		if (number != other.number) {
			return false;
		}
		return true;
	}

	public Present clone() {
		return new PresentImpl(number, getNames().clone(), category);
	}

	@Override
	public String toString() {
		return "Nummer: " + number + " ### " + names.toString();
	}

	@Override
	public boolean removeParticipant(String name) {
		return names.remove(name);
	}

	@Override
	public void addParticipant(String name) {
		names.add(name);
	}
}
